#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 64
int Register(void); // 登入函数
int main(int argc, char const *argv[])
{

  // 学生信息
  int sco[5] = {0};      // 成绩
  char buf[5][30] = {0}; // 等级
  int test = 0;          // 登入标识符
  char ch = 0;
  srand(time(NULL)); // 初始化随机数种子
  while (1)
  {
    test = Register(); // 登入
    if (test == 250)
      break;
    if (test == 2)
    {
      puts("成绩:");
      for (int i = 0; i < sizeof(sco) / sizeof(int); i++) // 输入并打印成绩
      {
        sco[i] = rand() % 100 + 1;
        printf("sco[%d]=%d\n", i, sco[i]);
      }
      // 排序成绩
      int temp = 0;
      for (int i = 0; i < sizeof(sco) / sizeof(int) - 1; i++)
      {
        for (int j = 0; j < sizeof(sco) / sizeof(int) - 1 - i; j++)
        {
          if (sco[j] < sco[j + 1])
          {
            temp = sco[j];
            sco[j] = sco[j + 1];
            sco[j + 1] = temp;
          }
        }
      }
      for (int i = 0; i < 5; i++)
      {
        if (sco[i] > 85)
          strcpy(buf[i], "优秀");
        else if (sco[i] <= 85 && sco[i] > 75)
          strcpy(buf[i], "良好");
        else if (sco[i] <= 75 && sco[i] >= 60)
          strcpy(buf[i], "及格");
        else if (sco[i] < 60)
          strcpy(buf[i], "不及格");
      }
      puts("开始输入学生成绩&等级\n");
      for (int i = 0; i < sizeof(sco) / sizeof(int); i++)
      {
        printf("sco[%d]=%d\t", i, sco[i]);
        printf("buf[%d]=%s\n", i, buf[i]);
      }
    }
    if (test != 2)
    {
      puts("输入Y/y开始查看成绩");
      getchar();
      scanf("%c", &ch);
      if (ch == 'y' || ch == 'Y')
      {
        puts("开始输入学生成绩&等级\n");
        for (int i = 0; i < sizeof(sco) / sizeof(int); i++)
        {
          printf("sco[%d]=%d\t", i, sco[i]);
          printf("buf[%d]=%s\n", i, buf[i]);
        }
      }
    }
    puts("输入Y/y切换账号 输入b退出系统\n");
    getchar();
    scanf("%c", &ch);
    if (ch == 'y' || ch == 'Y')
      continue;
    else if (ch == 'B' || ch == 'b')
      break;
  }
  puts("已退出系统，欢迎下次再来.\n");
  return 0;
}
int Register(void)
{
  char name[3][MAX] =
      {
          {"wxx"},
          {"wlz"},
          {"teacher"}};
  char pow[3][MAX] =
      {
          {"wxx2002"},
          {"wlz250"},
          {"hqyj2022"}};
  char my_name[MAX] = "";
  char my_pow[MAX] = "";
  int flag = 3;
  int test = 250;
  while (flag)
  {
    printf("请输入用户的名字:");
    scanf("%s", my_name);
    int i; // 获取登入名字数组下标
    for (i = 0; i < 3; i++)
    {
      if (!strcmp(my_name, name[i]))
        break;
    }
    if (i == 3)
    {
      flag--;
      printf("账号错误!你还有%d次机会\n", flag);
      continue;
    }
    else
    {
      printf("请输入%s账号的密码:\n", my_name);
      while (flag)
      {
        scanf("%s", my_pow);
        if (!strcmp(my_pow, pow[i]))
        {
          printf("账号%s登入成功!\n", my_name);
          test = i;
          flag = 0;
        }
        else
        {
          flag--;
          printf("密码错误！你还有%d次机会哦!\n", flag);
        }
      }
    }
  }
  return test;
}